/**
 * CEP Mobile Manager on Android October 2012 
 */
package com.orange.smartcity.android.tools;

import java.util.Iterator;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

/**
 * Helper Class JSON
 * 
 * @author FAIROUZ OUBENALI
 * 
 */
public class JsonUtils {

	/**
	 * converter map to json
	 * 
	 * @param map
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static JSONObject getJSON(Map map) {
		Iterator iter = map.entrySet().iterator();
		JSONObject holder = new JSONObject();

		while (iter.hasNext()) {
			Map.Entry pairs = (Map.Entry) iter.next();
			String key = (String) pairs.getKey();
			Map m = (Map) pairs.getValue();
			JSONObject data = new JSONObject();

			try {
				Iterator iter2 = m.entrySet().iterator();
				while (iter2.hasNext()) {
					Map.Entry pairs2 = (Map.Entry) iter2.next();
					data.put((String) pairs2.getKey(),
							(String) pairs2.getValue());
				}
				holder.put(key, data);
			} catch (JSONException e) {
				Log.e("Transforming", "There was an error packaging JSON", e);
			}
		}

		return holder;
	}
}
